//
//  ViewController.swift
//  mm
//
//  Created by mac on 16/4/5.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        nameTextField.delegate = self
        checkSaveButton()
        navigationItem.title = beauty?.name
        
        if let beauty = beauty {
            nameTextField.text = beauty.name
            imageAction.image = beauty.photo
            RantingControl.ranting = beauty.ranting
        }

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    
    @IBOutlet weak var nameTextField: UITextField!
    // MARK : action
    @IBAction func resetButton(sender: AnyObject) {
        NSLog("你点了按钮")
        
        
    }
    
    @IBAction func touchDown(sender: UIButton) {
        
    }
    @IBOutlet weak var imageAction: UIImageView!
    
    @IBOutlet weak var saveButton: UIBarButtonItem!
    
    @IBOutlet weak var RantingControl: Ranting!
    
    @IBAction func cancelBtn(sender: UIBarButtonItem) {
        let isAddMode = presentingViewController is UINavigationController
        if  isAddMode {
            dismissViewControllerAnimated(true, completion: nil)
        } else {
            navigationController!.popViewControllerAnimated(true)
        }
        
    }
    var beauty:Beauty?
  
    @IBAction func selectImage(sender: UITapGestureRecognizer) {
        let imagePicker = UIImagePickerController()
        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        imagePicker.delegate = self
        presentViewController(imagePicker, animated: true, completion: nil)
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if saveButton === sender {
            let name = nameTextField.text ?? ""
            let image = imageAction.image
            let ranting = RantingControl.ranting

            beauty = Beauty(name: name, photo: image, ranting: ranting)        }
    }
    func checkSaveButton() {
        let name = nameTextField.text ?? ""
      //  let rating = RantingControl.ranting
        if name.isEmpty  {
            saveButton.enabled = false
        } else {
            saveButton.enabled = true
        }
    }
    
    
 

}
extension ViewController: UITextFieldDelegate {
    func textFieldDidEndEditing(textField: UITextField) {
         navigationItem.title = nameTextField.text
        checkSaveButton()
        
    }
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        
        textField.resignFirstResponder()
        return true
      
    }
    func textFieldDidBeginEditing(textField: UITextField) {
        saveButton.enabled = false
    }

    
}
extension ViewController:UIImagePickerControllerDelegate {
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
        imageAction.image = selectedImage
        dismissViewControllerAnimated(true, completion: nil)
    }
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        dismissViewControllerAnimated(true, completion: nil)
    }

    
}
extension ViewController:UINavigationControllerDelegate {
    
}



